package com.jeesite.modules.task.deploy.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.io.FileUtils;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.ServiceException;
import com.jeesite.common.utils.SpringUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.annotation.ExcelField;
import com.jeesite.modules.annotation.RepeatSubmit;
import com.jeesite.modules.backlog.service.DcSupBacklogService;
import com.jeesite.modules.common.service.SysMsgService;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.service.FileUploadService;
import com.jeesite.modules.home.entity.DcSupTaskActionExplain;
import com.jeesite.modules.home.service.DcSupTaskActionExplainService;
import com.jeesite.modules.msg.entity.content.PcMsgContent;
import com.jeesite.modules.msg.utils.MsgPushUtils;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.EmpUtils;
import com.jeesite.modules.sys.utils.RoleUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.task.action.entity.DcSupTaskAction;
import com.jeesite.modules.task.action.service.DcSupTaskActionService;
import com.jeesite.modules.task.arrange.entity.DcSupTaskInfoArrange;
import com.jeesite.modules.task.arrange.service.DcSupTaskInfoArrangeService;
import com.jeesite.modules.task.audit.entity.DcSupTaskAudit;
import com.jeesite.modules.task.audit.service.DcSupTaskAuditService;
import com.jeesite.modules.task.deploy.entity.*;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoAddService;
import com.jeesite.modules.task.dict.entity.DcSupTaskInfoDict;
import com.jeesite.modules.task.dict.service.DcSupTaskInfoDictService;
import com.jeesite.modules.task.taskgroup.entity.DcSupTaskGroup;
import com.jeesite.modules.task.taskgroup.service.DcSupTaskGroupService;
import com.jeesite.modules.utils.Dict;
import com.jeesite.modules.utils.MinioUtils;
import com.jeesite.modules.utils.OfficeUtil;
import com.jeesite.modules.yzw.entity.MsgDetail;
import com.jeesite.modules.yzw.service.LehandYzwPushTask;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoService;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 任务部署信息表(主)Controller
 * @author zrr
 * @version 2022-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/taskdeploy/dcSupTaskInfo")
public class DcSupTaskInfoController extends BaseController {

	@Autowired
	private DcSupTaskInfoService dcSupTaskInfoService;
	@Autowired
	private DcSupTaskInfoAddService dcSupTaskInfoAddService;
	@Autowired
	private UserService userService;
	@Autowired
	private DcSupTaskInfoArrangeService dcSupTaskInfoArrangeService;
	@Autowired
	@Lazy
	private DcSupTaskAuditService dcSupTaskAuditService;
	@Autowired
	private DcSupTaskInfoDictService dcSupTaskInfoDictService;
	@Autowired
	private SysOfficeUnitService sysOfficeUnitService;
	@Autowired
	private DcSupTaskActionExplainService dcSupTaskActionExplainService;

	@Autowired
	private FileUploadService fileUploadService;

	@Autowired
	private DcSupTaskGroupService dcSupTaskGroupService;
	@Autowired
	private LehandYzwPushTask lehandYzwPushTask;
	@Autowired
	private DcSupTaskActionService dcSupTaskActionService;
	@Autowired
	private SysMsgService sysMsgService;
	@Autowired
	private DcSupBacklogService dcSupBacklogService;

	/**
	 * 获取数据
	 */
	public DcSupTaskInfo get(String id, boolean isNewRecord) {
		return dcSupTaskInfoService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(DcSupTaskInfo dcSupTaskInfo, Model model) {
		if(StringUtils.isNotBlank(dcSupTaskInfo.getId())){
			dcSupTaskInfo =dcSupTaskInfoService.get(dcSupTaskInfo);
		}

		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		model.addAttribute("dicts", dicts);
		return "modules/task/deploy/dcSupTaskInfoList";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = "listed")
	public String listed(DcSupTaskInfo dcSupTaskInfo, Model model) {
		if(StringUtils.isNotBlank(dcSupTaskInfo.getId())){
			dcSupTaskInfo =dcSupTaskInfoService.get(dcSupTaskInfo);
		}

		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		// 自动获取年度，近几年前台可自由配置
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		// 查询牵头领导
		User user = new User();
		user.setRoleCode("100016");
		user.setUserType("employee");
		List<User> list = userService.findList(user);
		model.addAttribute("userList", list);
		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);

		model.addAttribute("dicts", dicts);
		return "modules/task/deploy/dcSupTaskInfoedList";
	}



	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DcSupTaskInfo> listData(DcSupTaskInfo dcSupTaskInfo, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskInfo.setPage(new Page<>(request, response));
		Page<DcSupTaskInfo> page = dcSupTaskInfoService.findPage(dcSupTaskInfo);
		for (int i = 0; i < page.getList().size(); i++) {
			if(StringUtils.isNotBlank(page.getList().get(i).getMessageNumber())){
				page.getList().get(i).setMsgNum(page.getList().get(i).getMessageNumber().substring(0,page.getList().get(i).getMessageNumber().indexOf("〕")+1));
				page.getList().get(i).setMsgNum2(page.getList().get(i).getMessageNumber().substring(page.getList().get(i).getMessageNumber().indexOf("〕")+1));
			}
		}
		return page;
	}


	/**
	 * 重写查询列表数据
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = "queryListData")
	@ResponseBody
	public Page<DcSupTaskInfo> queryListData(DcSupTaskInfo dcSupTaskInfo, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskInfo.setPage(new Page<>(request, response));
//		if(RoleUtils.hasCurrentUserRole("province_task_watch")||UserUtils.getUser().getUserCode().equals("system")){
//			dcSupTaskInfo.setRemarks("0");
//		}else{
//			dcSupTaskInfo.setRemarks("1");
//		}
	//	Page<DcSupTaskInfo> page = dcSupTaskInfoService.findPage(dcSupTaskInfo);
		dcSupTaskInfo.setCreateUnit(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
		if (dcSupTaskInfo.getDataLimit().equals("0") &&EmpUtils.getEmployee().getEmpCode() !=null ){
			dcSupTaskInfo.setCreateBy(EmpUtils.getEmployee().getEmpCode());
		}
		Page<DcSupTaskInfo> page = dcSupTaskInfoService.findListPage(dcSupTaskInfo);
		for (int i = 0; i < page.getList().size(); i++) {
			if(StringUtils.isNotBlank(page.getList().get(i).getMessageNumber())){
				page.getList().get(i).setMsgNum(page.getList().get(i).getMessageNumber().substring(0,page.getList().get(i).getMessageNumber().indexOf("〕")+1));
				page.getList().get(i).setMsgNum2(page.getList().get(i).getMessageNumber().substring(page.getList().get(i).getMessageNumber().indexOf("〕")+1));
			}
		}
		return page;
	}
	
	/**
	 * 查询子表数据
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = "dcSupTaskActionListData")
	@ResponseBody
	public Page<DcSupTaskAction> subListData(DcSupTaskAction dcSupTaskAction, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskAction.setPage(new Page<>(request, response));
		Page<DcSupTaskAction> page = dcSupTaskInfoService.findSubPage(dcSupTaskAction);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = "form")
	public String form( DcSupTaskInfo dcSupTaskInfo, Model model) {
		DcSupTaskGroup dcSupTaskGroup = new DcSupTaskGroup();
		dcSupTaskGroup.setCreateBy(UserUtils.getUser().getUserCode());
		List<DcSupTaskGroup> groupList = dcSupTaskGroupService.findList(dcSupTaskGroup);
		model.addAttribute("groupList", groupList);
		// 获取任务的反馈时间节点
		if(StringUtils.isNotBlank(dcSupTaskInfo.getId())){
			dcSupTaskInfo =dcSupTaskInfoService.get(dcSupTaskInfo);
		}

		DcSupTaskInfoAdd dcSupTaskInfoAdd = new DcSupTaskInfoAdd();
		dcSupTaskInfoAdd.setTaskId(dcSupTaskInfo.getId());
		dcSupTaskInfo.setDcSupTaskInfoAddList(dcSupTaskInfoAddService.findList(dcSupTaskInfoAdd));
		model.addAttribute("auditContent", "无");
		if(StringUtils.inString(dcSupTaskInfo.getTaskStatus(),"3","4","5","6")){
			DcSupTaskAudit audit = new DcSupTaskAudit();
			audit.setTaskId(dcSupTaskInfo.getId());
			List<DcSupTaskAudit> dcSupTaskAudits = dcSupTaskAuditService.findList(audit);
			DcSupTaskAudit dcSupTaskAudit = dcSupTaskAudits.get(0);
			model.addAttribute("auditContent", dcSupTaskAudit.getAuditOption()==null?"无":dcSupTaskAudit.getAuditOption());
		}

		// 自动获取年度，近几年前台可自由配置
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);

		// 第一次打开form，默认为普通任务
		model.addAttribute("msgType", "dc_num_common");
		if (StringUtils.isBlank(dcSupTaskInfo.getNormalFlag())) {
			dcSupTaskInfo.setNormalFlag("1");
			model.addAttribute("msgType", "dc_num_common");
		} else if (StringUtils.equals("2", dcSupTaskInfo.getNormalFlag())) {
			// 秘书一科任务编辑时，回显督办科室
			List<Map> maps = officeSercretList();
			model.addAttribute("taskRequireOrg", maps);
			model.addAttribute("msgType", "dc_num_secret");
		}

		// 为联系人和联系单位赋初值
		User currUser = UserUtils.getUser();
		dcSupTaskInfo.setContactName(StringUtils.isBlank(dcSupTaskInfo.getContactName()) ? currUser.getUserName() : dcSupTaskInfo.getContactName());
		dcSupTaskInfo.setContactPhone(StringUtils.isBlank(dcSupTaskInfo.getContactPhone()) ? currUser.getMobile() : dcSupTaskInfo.getContactPhone());

		// 查询牵头领导
		User user = new User();
		user.setRoleCode("100016");
		user.setUserType("employee");
		List<User> list = userService.findList(user);

		// 初始化排序编号
		if (StringUtils.isBlank(dcSupTaskInfo.getSortNum())) {
			Integer maxSortNum = dcSupTaskInfoService.getMaxSortNum();
			if (maxSortNum == null) {
				maxSortNum = 1;
			} else {
				maxSortNum = maxSortNum + 10;
			}
			dcSupTaskInfo.setSortNum(dcSupTaskInfo.getSortNum() != null ? dcSupTaskInfo.getSortNum() : maxSortNum + "");
		}

		// 查询督办人列表
		user.setRoleCode("PARTY_MEMBER");
		List<User> taskSupUser = userService.findList(user);
		// 查询任务类型列表
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);

		// 查询审核人为秘书长的
		user.setRoleCode("SECRETARY_GENERAL");
		List<User> auditorUserList = userService.findList(user);

		DcSupTaskActionExplain explain = new DcSupTaskActionExplain();
		explain.setActionId(dcSupTaskInfo.getId());
		List<DcSupTaskActionExplain> explainList =  dcSupTaskActionExplainService.findList(explain);
		model.addAttribute("explainList", explainList);

		model.addAttribute("userList", list);
		model.addAttribute("taskSupUser", taskSupUser);
		model.addAttribute("dicts", dicts);
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		model.addAttribute("auditorUserList",auditorUserList);

		// 2已发布、3任务部署审核通过、5秘书长审核通过、7已确认、8进行中、9已结束
		if ("2".equals(dcSupTaskInfo.getTaskStatus()) || "3".equals(dcSupTaskInfo.getTaskStatus()) || "5".equals(dcSupTaskInfo.getTaskStatus())
				|| "7".equals(dcSupTaskInfo.getTaskStatus()) || "8".equals(dcSupTaskInfo.getTaskStatus()) || "9".equals(dcSupTaskInfo.getTaskStatus())
		) {
			return "modules/task/deploy/dcSupTaskInfoDetailForm";
		}
		return "modules/task/deploy/dcSupTaskInfoForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:edit")
	@PostMapping(value = "save")
	@RepeatSubmit
	@ResponseBody
	public String save( DcSupTaskInfo dcSupTaskInfo) {
		try {
			// 用户重新部署任务时，将此处的单条消息置为已读
			if("4".equals(dcSupTaskInfo.getTaskStatus()) || "6".equals(dcSupTaskInfo.getTaskStatus())){
				// 只有状态为任务部署审核驳回，或秘书长审核驳回的任务，执行此处
				sysMsgService.makeMsgReadByBiz(dcSupTaskInfo.getId(), "audit_reject_deploy", UserUtils.getUser().getUserCode());
			}

			// 获取当前会话对象
			dcSupTaskInfo.setCreateOrg(OfficeUtil.getCurrentOffice().getOfficeCode());
			dcSupTaskInfo.setCreateOrgName(OfficeUtil.getCurrentOffice().getOfficeName());
			dcSupTaskInfo.setDecomposeFlag("0");
			dcSupTaskInfo.setParentTaskId("0");
			dcSupTaskInfo.setSourceTaskId("0");
			dcSupTaskInfoService.save(dcSupTaskInfo);
			if(StringUtils.equals(dcSupTaskInfo.getTaskStatus(),"1")){
				return renderResult(Global.TRUE, text("保存草稿成功！"));
			}
			return renderResult(Global.TRUE, text("发布任务成功！"));
		} catch (Exception e) {
			e.printStackTrace();
			return renderResult(Global.FALSE, text(e.getMessage()));
		}
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(DcSupTaskInfo dcSupTaskInfo) {
		dcSupTaskInfo.setStatus(DcSupTaskInfo.STATUS_DISABLE);
		dcSupTaskInfoService.updateStatus(dcSupTaskInfo);
		return renderResult(Global.TRUE, text("停用任务部署信息成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(DcSupTaskInfo dcSupTaskInfo) {
		dcSupTaskInfo.setStatus(DcSupTaskInfo.STATUS_NORMAL);
		dcSupTaskInfoService.updateStatus(dcSupTaskInfo);
		return renderResult(Global.TRUE, text("启用任务部署信息成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DcSupTaskInfo dcSupTaskInfo) {

		dcSupTaskInfoService.delete(dcSupTaskInfo);
		return renderResult(Global.TRUE, text("删除任务部署信息成功！"));
	}

	@RequestMapping(value = "getMutiFeedBackTimes")
	@ResponseBody
	public String getMutiFeedBackTimes(DcSupTaskInfo scheBusiTaskInfo) throws ParseException {
		List<DcSupTaskInfoAdd> scheBusiTaskFeedbackList = dcSupTaskInfoService.getMutiFeedBackTimes(scheBusiTaskInfo);
		//return scheBusiTaskFeedbackList;
		if(scheBusiTaskFeedbackList.size()>0){
			return renderResult(Global.TRUE, text("多次反馈日期合理！"),scheBusiTaskFeedbackList);
		}else{
			return renderResult(Global.FALSE, text("请重新安排反馈日期或反馈方式！"));
		}


	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = "dcSupTaskConfirmList")
	public String confirmList(DcSupTaskInfo dcSupTaskInfo, Model model) {
		if(StringUtils.isNotBlank(dcSupTaskInfo.getId())){
			dcSupTaskInfo =dcSupTaskInfoService.get(dcSupTaskInfo);
		}

		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("dicts", dicts);
		// 查询牵头领导
		User user = new User();
		user.setRoleCode("100016");
		user.setUserType("employee");
		List<User> list = userService.findList(user);
		model.addAttribute("userList", list);
		return "modules/task/deploy/dcSupTaskConfirmList";
	}

	/**
	 * 查看确认任务表单
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = "confirmForm")
	public String confirmForm(DcSupTaskInfo dcSupTaskInfo, Model model) {
		if(StringUtils.isNotBlank(dcSupTaskInfo.getId())){
			dcSupTaskInfo =dcSupTaskInfoService.get(dcSupTaskInfo);
		}

		DcSupTaskInfoAdd dcSupTaskInfoAdd = new DcSupTaskInfoAdd();
		dcSupTaskInfoAdd.setTaskId(dcSupTaskInfo.getId());
		dcSupTaskInfo.setDcSupTaskInfoAddList(dcSupTaskInfoAddService.findList(dcSupTaskInfoAdd));
		// 自动获取年度，近几年前台可自由配置
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		Boolean flag = sysOfficeUnitService.checkSecretaryOther();
		if(flag) {
			List<Map> maps = officeSercretList();
			model.addAttribute("taskRequireOrg", maps);
		}
		User user  = new User();
		user.setRoleCode("100016");
		user.setUserType("employee");
		List<User> list = userService.findList(user);

		// 查询督办人列表
		user.setRoleCode("PARTY_MEMBER");
		List<User> taskSupUser = userService.findList(user);

		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("userList", list);
		model.addAttribute("taskSupUser", taskSupUser);
		model.addAttribute("dicts", dicts);
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		return "modules/task/deploy/dcSupTaskConfirm";
	}

	/**
	 * 查看确认任务表单
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = "confirm")
	@RepeatSubmit
	@ResponseBody
	public String confirm(@Validated DcSupTaskInfo dcSupTaskInfo) {
		// 用户确认任务时，将此处的多条消息置为已读
		sysMsgService.makeMsgReadByBiz(dcSupTaskInfo.getId() + "_" + dcSupTaskInfo.getTaskSupOrg(), "confirm_deploy", null);

		DcSupTaskInfo dcSupTaskInfoDb = dcSupTaskInfoService.get(dcSupTaskInfo.getId());
		if("7".equals(dcSupTaskInfoDb.getTaskStatus())){
			return renderResult(Global.TRUE, text("任务已被其他人确认！"));
		}
		dcSupTaskInfo.setTaskStatus("7");
		dcSupTaskInfoService.confirm(dcSupTaskInfo);
		return renderResult(Global.TRUE, text("任务已经确认！"));
	}

	/**
	 * 确认任务列表数据
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = "confirmListData")
	@ResponseBody
	public Page<DcSupTaskInfo> confirmListData(DcSupTaskInfo dcSupTaskInfo, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskInfo.setPage(new Page<>(request, response));
		Office currentOffice = OfficeUtil.getCurrentOffice();
		String officeCode = currentOffice.getOfficeCode();
		dcSupTaskInfo.setCreateOrg(officeCode);
		// 过滤管理员

		Page<DcSupTaskInfo> page = dcSupTaskInfoService.confirmListData(dcSupTaskInfo);

		return page;
	}

	/**
	 * 查看确认任务表单
	 */
	@RequestMapping(value = "detailForm")
	public String detailForm(DcSupTaskInfo dcSupTaskInfo, Model model) {
		String actionId = dcSupTaskInfo.getActionId();
		String leaderOrg = dcSupTaskInfo.getTaskLeaderOrg();
		if(StringUtils.isNotBlank(dcSupTaskInfo.getId())){
			dcSupTaskInfo =dcSupTaskInfoService.get(dcSupTaskInfo);
		}

		DcSupTaskInfoAdd dcSupTaskInfoAdd = new DcSupTaskInfoAdd();
		if (StringUtils.isNotBlank(leaderOrg)&&"3".equals(dcSupTaskInfo.getNormalFlag())){
			dcSupTaskInfoAdd.setLeaderOrg(leaderOrg);
		}
		if(dcSupTaskInfo!=null){
		dcSupTaskInfoAdd.setTaskId(dcSupTaskInfo.getId());}
		dcSupTaskInfo.setDcSupTaskInfoAddList(dcSupTaskInfoAddService.findList(dcSupTaskInfoAdd));
		dcSupTaskInfo.setFeedbackWay(OfficeUtil.getTaskRequire(dcSupTaskInfo.getFeedbackWay(),dcSupTaskInfo.getFeedbackRate()));
		if(dcSupTaskInfo.getRemarks4()==null){
			dcSupTaskInfo.setRemarks4("");
		}
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		DcSupTaskActionExplain explain = new DcSupTaskActionExplain();
		explain.setActionId(dcSupTaskInfo.getId());
		List<DcSupTaskActionExplain> explainList =  dcSupTaskActionExplainService.findList(explain);
		model.addAttribute("explainList", explainList);
		DcSupTaskAction action = dcSupTaskActionService.get(new DcSupTaskAction(actionId));
		model.addAttribute("action",action!=null?action:"");
		if("3".equals(dcSupTaskInfo.getNormalFlag())&&StringUtils.isNotBlank(leaderOrg)){
			return "modules/task/deploy/dcSupTaskInfoDetailFormxihua";
		}else{
			return "modules/task/deploy/dcSupTaskInfoDetailForm";
		}
	}

    public List<Map> officeSercretList(){
        List<Map>  list = dcSupTaskInfoService.officeSercretList();
        return list;
    }
	public Map officeSercretFirst(){
		Map  map = dcSupTaskInfoService.officeSercretFirst();
		return map;
	}


	/**
	 * 查看确认任务表单
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = "arrangeTask")
	public String arrangeTask(DcSupTaskInfoArrange arrange,DcSupTaskInfo dcSupTaskInfo, Model model) {
		List<Map> list  = dcSupTaskInfoService.queryOrgLastedFeedBackContentList(arrange.getTaskId());
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).get("feedback_advice")==null){
				list.get(i).put("feedback_advice","");
			}
			if(list.get(i).get("feedback_measure")==null){
				list.get(i).put("feedback_measure","");
			}
		}
		if(StringUtils.isNotBlank(arrange.getTaskId())){
			dcSupTaskInfo.setId(arrange.getTaskId());
			dcSupTaskInfo =dcSupTaskInfoService.get(dcSupTaskInfo);
		}
		model.addAttribute("arrange", arrange);
		model.addAttribute("maps", list);
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		return "modules/task/deploy/dcSupTaskArrange";
	}

	/**
	 * 查看确认任务表单
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = "saveArrange")
	@ResponseBody
	@RepeatSubmit
	public String saveArrange(@Validated DcSupTaskInfoArrange arrange) {
		// 用户提交任务整理时，将此处的单条消息置为已读
		sysMsgService.makeMsgReadByBiz(arrange.getTaskId(), "audit_reject_arrange", UserUtils.getUser().getUserCode());

		User user = UserUtils.getUser();
		DcSupTaskInfo dcSupTaskInfo = new DcSupTaskInfo();
		dcSupTaskInfo.setId(arrange.getTaskId());
		dcSupTaskInfo = dcSupTaskInfoService.get(dcSupTaskInfo);
		arrange.setArrangeBy(user.getUserCode());
		arrange.setArrangeName(user.getUserName());
		arrange.setArrangeTime(new Date());

		// 设置一个临时变量来记录这条整理放到审核表中时的id，在消息中使用
		String tempAuditId = "";

		//普通任务和细化分解任务默认审核通过
		if ("1".equals(dcSupTaskInfo.getNormalFlag()) || "3".equals(dcSupTaskInfo.getNormalFlag())) {
			//默认无需审核
			arrange.setAuditFlag("0");
			arrange.setArrangeStatus("2");
			dcSupTaskInfo.setTaskProgress(arrange.getTaskArrangeStatus());
			dcSupTaskInfoService.update(dcSupTaskInfo);
			dcSupTaskInfoArrangeService.save(arrange);
		} else if ("2".equals(dcSupTaskInfo.getNormalFlag()) && "0".equals(arrange.getAuditFlag())) {
			arrange.setArrangeStatus("2");
			dcSupTaskInfo.setTaskProgress(arrange.getTaskArrangeStatus());
			dcSupTaskInfoService.update(dcSupTaskInfo);
			dcSupTaskInfoArrangeService.save(arrange);
		} else {
			arrange.setArrangeStatus("1");
			dcSupTaskInfoArrangeService.save(arrange);
			tempAuditId = IdGen.nextId();
			dcSupTaskAuditService.addAuditInfo(tempAuditId, arrange.getId(), arrange.getTaskId(), Dict.TargetType.TYPE_3, Dict.AuditType.TYPE_6, dcSupTaskInfo.getTaskSupUser(), dcSupTaskInfo.getTaskSupUserName(), "PARTY_MEMBER");
		}

		if ("2".equals(dcSupTaskInfo.getNormalFlag()) && "1".equals(arrange.getAuditFlag())) {
			// 11秘提交整理后，向任务督办人（即党组机关成员）发送消息，提醒审核
//			PcMsgContent msg = new PcMsgContent();
//			msg.setTitle("提示信息");
//			msg.setContent("您有一个任务:\"" + dcSupTaskInfo.getTaskTitle() + "\"已整理,请及时审核");
//			msg.addButton("任务整理审核", "/a/audit/dcSupTaskAudit/form?id=" + tempAuditId);
//			MsgPushUtils.push(msg, tempAuditId, "audit_arrange", dcSupTaskInfo.getTaskSupUser());
			//发送易政网消息
			MsgDetail msgDetail = new MsgDetail();
			msgDetail.setSendid(UserUtils.getUser().getUserCode());
			msgDetail.setSubjectid(dcSupTaskInfo.getTaskSupUser());
			msgDetail.setMesg("您有一个任务:\"" + dcSupTaskInfo.getTaskTitle() + "\"已整理,请及时登录系统审核");
			lehandYzwPushTask.pushYzwShortMsg(msgDetail);
			//任务整理创建待办
			dcSupBacklogService.add("督查督办","任务整理审核","任务整理审核","提示信息"
					,"您有一个任务:" + dcSupTaskInfo.getTaskTitle() + "已整理,请及时审核"
					,"/a/audit/dcSupTaskAudit/form?id=" + tempAuditId,""
					,tempAuditId,"task_audit_arrange",dcSupTaskInfo.getTaskSupUser()
					,dcSupTaskInfo.getId(),"");
		}
		return renderResult(Global.TRUE, text("整理任务成功！"));
	}

	/**
	 * 秘书一科多任务表单
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:view")
	@RequestMapping(value = "multiForm")
	public String multiForm( DcSupTaskInfo dcSupTaskInfo, Model model) {
		// 获取任务的反馈时间节点
		DcSupTaskGroup dcSupTaskGroup = new DcSupTaskGroup();
		dcSupTaskGroup.setCreateBy(UserUtils.getUser().getUserCode());
		List<DcSupTaskGroup> groupList = dcSupTaskGroupService.findList(dcSupTaskGroup);
		model.addAttribute("groupList", groupList);
		if(StringUtils.isNotBlank(dcSupTaskInfo.getId())){
			dcSupTaskInfo =dcSupTaskInfoService.get(dcSupTaskInfo);
		}

		DcSupTaskInfoAdd dcSupTaskInfoAdd = new DcSupTaskInfoAdd();
		dcSupTaskInfoAdd.setTaskId(dcSupTaskInfo.getId());
		dcSupTaskInfo.setDcSupTaskInfoAddList(dcSupTaskInfoAddService.findList(dcSupTaskInfoAdd));

		// 自动获取年度，近几年前台可自由配置
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);

		// 第一次打开form，默认为普通任务
		model.addAttribute("msgType", "dc_num_common");
		if (StringUtils.isBlank(dcSupTaskInfo.getNormalFlag())) {
			dcSupTaskInfo.setNormalFlag("1");
			model.addAttribute("msgType", "dc_num_common");
		} else if (StringUtils.equals("2", dcSupTaskInfo.getNormalFlag())) {
			// 秘书一科任务编辑时，回显督办科室
			List<Map> maps = officeSercretList();
			model.addAttribute("taskRequireOrg", maps);
			model.addAttribute("msgType", "dc_num_secret");
		}

		// 为联系人和联系单位赋初值
//		User currUser = UserUtils.getUser();
//		dcSupTaskInfo.setContactName(StringUtils.isBlank(dcSupTaskInfo.getContactName()) ? currUser.getUserName() : dcSupTaskInfo.getContactName());
//		dcSupTaskInfo.setContactPhone(StringUtils.isBlank(dcSupTaskInfo.getContactPhone()) ? currUser.getMobile() : dcSupTaskInfo.getContactPhone());

		// 查询牵头领导
		User user = new User();
		user.setRoleCode("100016");
		user.setUserType("employee");
		List<User> list = userService.findList(user);

		// 初始化排序编号
		if (StringUtils.isBlank(dcSupTaskInfo.getSortNum())) {
			Integer maxSortNum = dcSupTaskInfoService.getMaxSortNum();
			if (maxSortNum == null) {
				maxSortNum = 1;
			} else {
				maxSortNum = maxSortNum + 10;
			}
			dcSupTaskInfo.setSortNum(dcSupTaskInfo.getSortNum() != null ? dcSupTaskInfo.getSortNum() : maxSortNum + "");
		}
		Integer  messageNumber  = dcSupTaskInfoService.getMaxMessageNumber();
		if(StringUtils.isBlank(dcSupTaskInfo.getMessageNumber())){
			if(messageNumber==null){messageNumber=0;}
			dcSupTaskInfo.setMessageNumber(messageNumber+1+"");
		}else{
			if(dcSupTaskInfo.getMessageNumber().contains("〕")){
				dcSupTaskInfo.setMessageNumber(dcSupTaskInfo.getMessageNumber().split("〕")[1]);}
		}
		// 查询督办人列表
		user.setRoleCode("PARTY_MEMBER");
		List<User> taskSupUser = userService.findList(user);

		// 查询任务类型列表
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		// 查询审核人为秘书长的
		user.setRoleCode("SECRETARY_GENERAL");
		List<User> auditorUserList = userService.findList(user);
		User userOffLine = new User();
		userOffLine.setUserCode("0000");
		userOffLine.setUserName("线下审核");
		auditorUserList.add(userOffLine);

		model.addAttribute("userList", list);
		model.addAttribute("taskSupUser", taskSupUser);
		model.addAttribute("dicts", dicts);
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);

		model.addAttribute("auditorUserList",auditorUserList);
		DcSupTaskActionExplain explain = new DcSupTaskActionExplain();
		explain.setActionId(dcSupTaskInfo.getId());
		List<DcSupTaskActionExplain> explainList =  dcSupTaskActionExplainService.findList(explain);
		model.addAttribute("explainList", explainList);
		if("2".equals(dcSupTaskInfo.getTaskStatus())||"3".equals(dcSupTaskInfo.getTaskStatus())){
			return "modules/task/deploy/dcSupTaskInfoDetailForm";
		}
		return "modules/task/deploy/dcSupTaskInfoMultiForm";
	}



	/**
	 * 保存数据
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfo:edit")
	@PostMapping(value = "batchSave")
	@ResponseBody
	@RepeatSubmit
	public String batchSave( DcSupTaskInfoMulti dcSupTaskInfo) {
		try {
			// 获取当前会话对象
			List<String> taskIds = dcSupTaskInfoService.multiSave(dcSupTaskInfo);
			// 将附件添加到所有的任务中
			dcSupTaskInfoService.addAttachmentToAllInfo(taskIds);
			if(StringUtils.equals(dcSupTaskInfo.getTaskStatus(),"1")){
				return renderResult(Global.TRUE, text("保存草稿成功！"));
			}
			return renderResult(Global.TRUE, text("发布任务成功！"));
		} catch (Exception e) {
			return renderResult(Global.FALSE, text(e.getMessage()));
		}
	}


	/**
	 * 下载导入督办数据模板
	 */
	@RequestMapping(value = "importTemplate")
	public void importTemplate(HttpServletResponse response) {
		DcSupTaskInfo dcSupTaskInfo= new DcSupTaskInfo();
		dcSupTaskInfo.setTaskYear("2022");
		dcSupTaskInfo.setTaskTypeName("国务院大督查");
		dcSupTaskInfo.setTaskTitle("我是标题");
		dcSupTaskInfo.setTaskContent("我是内容");
		dcSupTaskInfo.setTaskStartTime(new Date());
		dcSupTaskInfo.setTaskEndTime(new Date());
		dcSupTaskInfo.setSortNum("1");
		List<DcSupTaskInfo> list = ListUtils.newArrayList(dcSupTaskInfo);
		String fileName = "督办数据模板.xlsx";
		try(ExcelExport ee = new ExcelExport("督办数据", DcSupTaskInfo.class, ExcelField.Type.IMPORT)){
			ee.setDataList(list).write(response, fileName);
		}
	}


	/**
	 * 导入督办数据
	 */
	@ResponseBody
	@PostMapping(value = "importData")
	public String importData(MultipartFile file, String normalFlag) {
		try {
			boolean isNormalFlag = Global.YES.equals(normalFlag);
			String message = dcSupTaskInfoService.importData(file,isNormalFlag);
			return renderResult(Global.TRUE, "posfull:"+message);
		} catch (Exception ex) {
			return renderResult(Global.FALSE, "posfull:"+ex.getMessage());
		}
	}
	/**
	 * 删除草稿任务
	 */
	@RequestMapping(value = "deleteById")
	@ResponseBody
	public String deleteById(DcSupTaskInfo dcSupTaskInfo) {

		dcSupTaskInfoService.deleteById(dcSupTaskInfo);
		return renderResult(Global.TRUE, text("删除细化任务成功！"));
	}

	@RequestMapping(value = "queryLeaderListApp")
	@ResponseBody
	public String queryLeaderListApp() {
		User user = new User();
		user.setRoleCode("100016");
		user.setUserType("employee");
		List<User> list = userService.findList(user);
		return renderResult(Global.TRUE, text("成功！"),list);
	}
	//APP端获取督办字号
	@RequestMapping(value = "getNumber")
	@ResponseBody
	public Integer getNumber() {
		return dcSupTaskInfoService.getMaxMessageNumber()+1;
	}



	@RequestMapping(value = "queryDownLoadDocument")
	@ResponseBody
	public void queryDownLoadDocument( String id,String bizType,String jpgId, HttpServletResponse response){
		FileUpload fileUpload =new FileUpload();
		fileUpload.setBizKey(id);
		fileUpload.setBizType(bizType);
		List<FileUpload> list = fileUploadService.findList(fileUpload);
		if (CollectionUtils.isEmpty(list)){
			if (StringUtils.isNotBlank(jpgId)){
				list = new ArrayList<>();
				String[] split = jpgId.split(",");
				for (int i = 0; i < split.length; i++) {
					FileUpload upload = fileUploadService.get(split[i]);
					list.add(upload);
				}
			}
		}
		if (list !=null && list.size() >0) {
			MinioUtils minioUtils = SpringUtils.getBean(MinioUtils.class);
			ZipOutputStream zos =null;
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/x-msdownload");
			long time = new Date().getTime();
			response.setHeader("Content-disposition","attachment;filename="+time+".zip");
			try {
				zos =new ZipOutputStream(response.getOutputStream());
				for (int i = 0; i < list.size(); i++) {
					FileUpload data = list.get(i);
					// 文件存放地址
					String fileFullName = ClassUtils.getDefaultClassLoader().getResource("").getPath() + data.getFileName();
					minioUtils.getRemoteFile(data.getFileEntity(), fileFullName);
					BufferedInputStream stream = new BufferedInputStream(new FileInputStream(fileFullName));
					byte[] buf =new byte[1024];
					zos.putNextEntry(new ZipEntry(data.getFileName()));
					int len;
					while((len = stream.read(buf))!=-1){
						zos.write(buf,0,len);
					}
					zos.closeEntry();
					FileUtils.deleteFile(fileFullName);
				}
				System.out.println("文件压缩成功");
			}catch (Exception e){
				e.printStackTrace();
				response.setStatus(500);
				throw new ServiceException("下载minio远程文件出错！");
			}finally {
				if (zos !=null){
					try {
						zos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		}
	}


	private  DcSupTaskInfo query(String id){
		return dcSupTaskInfoService.get(id);
	}

	@RequestMapping(value = "downLoadOverseeTheSingle")
	@ResponseBody
	public void downLoadOverseeTheSingle(DcSupTaskInfo dcSupTaskInfo,String id,String remarks4,HttpServletRequest request,HttpServletResponse response) throws Exception {
	 		if (StringUtils.isBlank(id)){
				throw new ServiceException("导出word文件出错！");
			}
		dcSupTaskInfo=query(id.split(",")[0]);
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setDictValue(dcSupTaskInfo.getTaskTypeCode());
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);

		// 定义默认的导出文件名 督办单-秘书科/督查办
		String fileName = "督办单-%s" + DateUtils.getDate("yyyyMMddHHmmss");
		String documentName="";
		Map<String,Object> map =new HashMap<>();
		// 仿宋_GB2312 四号字体=14  任务名称
		map.put("taskTitle",new TextRenderData(dcSupTaskInfo.getTaskTitle(),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
		// 督办字号
		map.put("messageNumber",new TextRenderData(dcSupTaskInfo.getMessageNumber(),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
		// 督办类别
		map.put("taskTypeName",new TextRenderData(dicts.get(0).getDictLabel(),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
		// 督办内容
		map.put("taskContent",new TextRenderData(dcSupTaskInfo.getTaskContent(),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
		// 联系人
		map.put("contactName",new TextRenderData(dcSupTaskInfo.getContactName(),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
		// 联系电话
		map.put("contactPhone",new TextRenderData(dcSupTaskInfo.getContactPhone(),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
		// 创建时间
		map.put("createDate",new TextRenderData(DateUtils.formatDate(dcSupTaskInfo.getCreateDate()),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
		// 秘书科 -- 秘书一科任务 -- 在闭环管理任务中
		if (dcSupTaskInfo.getNormalFlag().equals("2")){
			fileName =String.format(fileName,"秘书科");
			documentName="oversee-secretarial.docx";
			// 生成日期
			map.put("date",new TextRenderData(DateUtils.formatDate(new Date(),"yyyy年MM月dd日"),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(16).build()));
			// 反馈日期
			map.put("endDate",new TextRenderData(DateUtils.formatDate(dcSupTaskInfo.getTaskEndTime(),"MM月dd日"),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(16).build()));
		// 督查办 -- 普通任务 -- 督办任务部署中
		}else{
			map.put("red",new TextRenderData(dcSupTaskInfo.getRemarks4().equals("1")?"红色":(dcSupTaskInfo.getRemarks4().equals("2")?"黄色":"蓝色"),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
			map.put("taskRequireName",new TextRenderData(dcSupTaskInfo.getTaskRequireName(),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
			fileName =String.format(fileName,"督查办");
			documentName="oversee_office.docx";
			// 责任单位
			map.put("taskLeaderOrgBase",new TextRenderData(dcSupTaskInfo.getTaskLeaderOrgBase(),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
		}
		String path = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "/template/";
		XWPFTemplate template = XWPFTemplate.compile(path+documentName).render(map);
		String tempFile = path + fileName + ".docx";
		try {
			template.writeToFile(tempFile);
		} catch (IOException e) {
			logger.error("导出word文件出错！", e);
			throw new ServiceException("导出word文件出错！");
		}
		FileUtils.downFile(new File(tempFile), request, response, fileName + ".docx");
		FileUtils.deleteFile(tempFile);
	}


	@RequestMapping(value = "downLoadOversee")
	@ResponseBody
	public void downLoadOversee(DcSupTaskInfoMulti dcSupTaskInfo,HttpServletRequest request,HttpServletResponse response) throws Exception {
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setDictValue(dcSupTaskInfo.getTaskTypeCode());
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);

		List<Map<String,String>> list = createDocument(dcSupTaskInfo, dicts);
		// 如果是单个文件 则单个文件下载
		if (!list.isEmpty() &&list.size() <2){
			Map<String, String> map = list.get(0);
			FileUtils.downFile(new File(map.get("tempFile")), request, response, map.get("fileName") + ".docx");
			FileUtils.deleteFile(map.get("tempFile"));
		}else{
			ZipOutputStream zos =null;
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/x-msdownload");
			long time = new Date().getTime();
			response.setHeader("Content-disposition","attachment;filename="+time+".zip");
			try {
				zos =new ZipOutputStream(response.getOutputStream());
				for (int i = 0; i < list.size(); i++) {
					Map<String,String> data = list.get(i);
					// 文件存放地址
//					String fileFullName = ClassUtils.getDefaultClassLoader().getResource("").getPath() + data.get("fileName");
					BufferedInputStream stream = new BufferedInputStream(new FileInputStream(data.get("tempFile")));
					byte[] buf =new byte[1024];
					zos.putNextEntry(new ZipEntry(data.get("fileName")+".docx"));
					int len;
					while((len = stream.read(buf))!=-1){
						zos.write(buf,0,len);
					}
					zos.closeEntry();
					FileUtils.deleteFile(data.get("tempFile"));
				}
				System.out.println("文件压缩成功");
			}catch (Exception e){
				e.printStackTrace();
				response.setStatus(500);
				throw new ServiceException("下载文件出错！");
			}finally {
				if (zos !=null){
					try {
						zos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}


	private List<Map<String,String> > createDocument(DcSupTaskInfoMulti dcSupTaskInfo,List<DcSupTaskInfoDict> dicts){
		List<String> contents = dcSupTaskInfo.getTaskContent().stream().filter(s -> StringUtils.isNotBlank(s)).collect(Collectors.toList());
		List<Map<String,String>> list =new ArrayList<>();
		// 如果督办内容不为空
		for (int i = 0; i < contents.size(); i++) {
			String fileName = "督办单-%s" + DateUtils.getDate("yyyyMMddHHmmss")+i;
			String documentName="";
			Map<String,Object> map =new HashMap<>();
			// 仿宋_GB2312 四号字体=14  任务名称
			map.put("taskTitle",new TextRenderData(dcSupTaskInfo.getTaskTitle(),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
			// 督办字号
			map.put("messageNumber",new TextRenderData(dcSupTaskInfo.getMessageNumber(),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
			// 督办类别
			map.put("taskTypeName",new TextRenderData(dicts.get(0).getDictLabel(),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
			// 督办内容
			map.put("taskContent",new TextRenderData(dcSupTaskInfo.getTaskContent().get(i),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
			// 联系人
			map.put("contactName",new TextRenderData(dcSupTaskInfo.getContactName().get(i),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
			// 联系电话
			map.put("contactPhone",new TextRenderData(dcSupTaskInfo.getContactPhone().get(i),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));

			// 秘书科 -- 秘书一科任务 -- 在闭环管理任务中
			if (dcSupTaskInfo.getRemarks4().equals("1")){
				fileName =String.format(fileName,"秘书科");
				documentName="oversee-secretarial.docx";
				// 生成日期
				map.put("date",new TextRenderData(DateUtils.formatDate(new Date(),"yyyy年MM月dd日"),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(16).build()));
				// 反馈日期
				map.put("endDate",new TextRenderData(DateUtils.formatDate(dcSupTaskInfo.getTaskEndTime().get(i),"MM月dd日"),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(16).build()));
				// 督查办 -- 普通任务 -- 督办任务部署中
			}else {
				fileName =String.format(fileName,"督查办");
				documentName="oversee_office.docx";
				// 责任单位
				map.put("taskLeaderOrgBase",new TextRenderData(dcSupTaskInfo.getTaskLeaderOrgName().get(i),Style.builder().buildFontFamily("仿宋_GB2312").buildFontSize(14).build()));
			}
			String path = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "/template/";
			XWPFTemplate template = XWPFTemplate.compile(path+documentName).render(map);
			String tempFile = path + fileName + ".docx";
			try {
				template.writeToFile(tempFile);
			} catch (IOException e) {
				logger.error("导出word文件出错！", e);
				throw new ServiceException("导出word文件出错！");
			}
			Map<String,String> info =new HashMap<>();
			info.put("fileName",fileName);
			info.put("tempFile",tempFile);
			list.add(info);
		}
		return  list;
	}
	@RequestMapping(value = "getMaxMessageNumberByType")
	@ResponseBody
	public String getMaxMessageNumberByType(DcSupTaskInfoMsgNum dcSupTaskInfoMsgNum){
		DcSupTaskInfoMsgNum res = dcSupTaskInfoService.getMaxMessageNumberByType(dcSupTaskInfoMsgNum);
		if(res==null){
			res = new DcSupTaskInfoMsgNum();
			res.setMsgNum2("0");
		}
		return renderResult(Global.TRUE, text("整理任务成功！"),res);
	}

	@RequestMapping(value = "updateTaskLeader")
	@ResponseBody
	public String updateTaskLeader(DcSupTaskInfo dcSupTaskInfo){
		try {
			dcSupTaskInfoService.update(dcSupTaskInfo);
			return renderResult(Global.TRUE, text("选择领导人成功"));
		} catch (Exception e) {
			e.printStackTrace();
			return renderResult(Global.FALSE, text("选择领导人失败"));
		}
	}

	@RequestMapping(value = "deleteList")
	public String deleteList(DcSupTaskInfo dcSupTaskInfo, Model model) {
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		return "modules/task/delete/dcSupTaskInfoedDeleteList";
	}

	@RequestMapping(value = "queryDeleteListData")
	@ResponseBody
	public Page<DcSupTaskInfo> queryDeleteListData(DcSupTaskInfo dcSupTaskInfo, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskInfo.setPage(new Page<>(request, response));
		Page<DcSupTaskInfo> page = dcSupTaskInfoService.queryDeleteListData(dcSupTaskInfo);
		return page;
	}

	@RequestMapping(value = "deleteAll")
	@ResponseBody
	public String deleteAll(DcSupTaskInfo dcSupTaskInfo) {
		dcSupTaskInfoService.deleteAll(dcSupTaskInfo);
		return renderResult(Global.TRUE, text("删除任务成功！"));
	}

	/**
	 * 移动端催办列表
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getActionFeedback")
	@ResponseBody
	public String getActionFeedback(String id) {
		return renderResult(Global.TRUE, text("查询成功！"),dcSupTaskInfoService.getActionFeedback(id));
	}


	/** 同步成功
	 * @return
	 */
	@RequestMapping(value = "syncTaskActionAndFeedBack")
	@ResponseBody
	public String syncTaskActionAndFeedBack(String pass) {
		if(pass==null){
			return renderResult(Global.FALSE, text("同步失败！"));
		}
		dcSupTaskInfoService.syncTaskActionAndFeedBack();
		return renderResult(Global.TRUE, text("同步成功！"));
	}


	/**
	 * 牵头单位明细
	 * @param
	 * @return
	 */
	@RequestMapping(value = "showTaskLeaderOrg")
	public String showTaskLeaderOrg(Model model,String taskNo){
		model.addAttribute("taskNo",taskNo);
		model.addAttribute("btnStatus",false);
		Date time  = dcSupTaskInfoService.minFeedbackEndTime(taskNo);
		if(time!=null && time.getTime()>new Date().getTime()){
			model.addAttribute("btnStatus",true);
		}
		return "modules/task/deploy/taskLeaderOrgList";
	}

	/**
	 * 牵头单位
	 * @param
	 * @return
	 */
	@RequestMapping(value = "taskLeaderOrgList")
	@ResponseBody
	public Page<Map> taskLeaderOrgList(String taskNo){
		Page<Map> page = new Page<>();
	//	List<Map>  list =dcSupTaskActionService.findLeaderOrg(taskNo);
		DcSupTaskInfo dcSupTaskInfo =dcSupTaskInfoService.get(taskNo);
		if(dcSupTaskInfo!=null){
			String[] splits = dcSupTaskInfo.getTaskLeaderOrgName().split(",");
			List<Map>  list =	new ArrayList();
			for(String org:splits){
				Map map = new HashMap();
				map.put("leader_org_name",org);
				list.add(map);
			}
			page.setList(list);
		}
		return page;
	}

	/**
	 * 追加牵头单位
	 * @param
	 * @return
	 */
	@RequestMapping(value = "followOrgForm")
	public String followOrgForm(Model model,String taskNo){
		model.addAttribute("taskNo",taskNo);
		return "modules/task/deploy/followOrgForm";
	}


	/**
	 * 保存追加牵头单位
	 * @param
	 * @return
	 */
	@RequestMapping(value = "savefollowOrg")
	@ResponseBody
	public String savefollowOrg(String taskNo,String unitCodes,String unitNames){
		DcSupTaskInfo dcSupTaskInfo =dcSupTaskInfoService.get(taskNo);
		String[] orgNames = dcSupTaskInfo.getTaskLeaderOrgName().split(",");
		String[] orgnames_ = unitNames.split(",");
		List<String> orgs = Stream.of(orgNames).collect(Collectors.toList());
		List<String> orgs_ = Stream.of(orgnames_).collect(Collectors.toList());
		 orgs.retainAll(orgs_);
		 if(orgs!=null && orgs.size()>0){
			 return renderResult(Global.FALSE, text("追加失败！【"+orgs.toString()+"】这些牵头单位重复"));
		 }
		dcSupTaskInfoService.savefollowOrg(dcSupTaskInfo,unitCodes,unitNames);
		return renderResult(Global.TRUE, text("追加成功！"));
	}

	@RequestMapping(value = "editAllInfo")
	public String editAllInfo(Model model,String id){
		DcSupTaskInfo info = this.get(id,false);
		model.addAttribute("editAllInfo",info);
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("dicts", dicts);
		if(StringUtils.equals(info.getNormalFlag(),"2")){
			model.addAttribute("msgType", "dc_num_secret");
		}else{
			model.addAttribute("msgType", "dc_num_common");
		}
		return "modules/task/delete/editAllInfo";
	}

	@RequiresPermissions("taskdeploy:dcSupTaskInfo:edit")
	@PostMapping(value = "saveEditAllInfo")
	@RepeatSubmit
	@ResponseBody
	public String saveEditAllInfo( DcSupTaskInfo dcSupTaskInfo) {
		try {
			dcSupTaskInfoService.saveEditAllInfo(dcSupTaskInfo);
			return renderResult(Global.TRUE, text("修改任务成功！"));
		} catch (Exception e) {
			e.printStackTrace();
			return renderResult(Global.FALSE, text(e.getMessage()));
		}
	}

}